/**
 * 
 */
var $RTC_marker_dialog = {
    form_inputs:[],
    feedback_element:null,
    close_callback:null,
    action_canceled:false,
    delete_clicked:false,

    init:function(config) {//form_input_ids,feedback_element_id,dialog_id) {
	var dialog_id=config['dialog_id']||null;
	var feedback_element_id=config['feedback_element_id']||null;
	var form_input_ids=config['form_input_ids']||[];
	this.close_callback=config['close_callback']||null;
	if(!dialog_id||!feedback_element_id||!form_input_ids||!this.close_callback) {
	    $RTC_log.error("Missing required args for $RTC_marker_dialog.init", $RTC_marker_dialog);
	}
	$(form_input_ids).each(function(index,item){
	    $RTC_marker_dialog[item] = $('#'+item);
	    $RTC_log.debug($('#'+item));
	    $RTC_marker_dialog.form_inputs.push($RTC_marker_dialog[item]);
	});
	$RTC_marker_dialog.feedback_element=$("#"+feedback_element_id);
	$RTC_log.debug($RTC_marker_dialog.form_inputs);

	$("#"+dialog_id).dialog({
	    autoOpen: false,
	    height: 400,
	    width: 400,
	    modal: true,
	    buttons: {
		'Create Marker': function() {
		    $RTC_marker_dialog.clear_form_errors();
		    var bValid = true;
		    $($RTC_marker_dialog.form_inputs).removeClass('ui-state-error');
		    bValid = bValid && $RTC_marker_dialog.checkLength($RTC_marker_dialog.marker_label,"Label",1,50);
		    //bValid = bValid && $RTC_marker_dialog.checkRegexp($RTC_marker_dialog.marker_size,/^[0-9]+\.?[0-9]*$/,"The marker size should be numeric");
		    if (bValid) {
			$(this).dialog('close');
		    }
		},
		Cancel: function() {
		    $RTC_marker_dialog.clear_form();
		    $RTC_marker_dialog.action_canceled=true;
		    $RTC_marker_dialog.delete_clicked=false;
		    $(this).dialog('close');
		},
		'Delete Marker' : function() {
		    $RTC_marker_dialog.delete_clicked=true;
		    $(this).dialog('close');
		}
	    },
	    close: function(event, ui) {
		// report values back to confab
		$RTC_log.debug($RTC_marker_dialog);
		if(!$RTC_marker_dialog.action_canceled) {
		    $RTC_marker_dialog.close_callback($RTC_marker_dialog.all_field_values(),$RTC_marker_dialog.delete_clicked);
		}
		//confab.retrieve_dialog_values($RTC_marker_dialog.all_field_values());
		$RTC_marker_dialog.clear_form();
	    }
	});
	$('#create-user').click(function() {
	    $('#dialog').dialog('open');
	})
	.hover(
	    function(){
		$(this).addClass("ui-state-hover");
	    },
	    function(){
		$(this).removeClass("ui-state-hover");
	    }
	    ).mousedown(function(){
	    $(this).addClass("ui-state-active");
	})
	.mouseup(function(){
	    $(this).removeClass("ui-state-active");
	});

    },
    clear_form:function() {
	$RTC_marker_dialog.action_canceled=false;
	$RTC_marker_dialog.delete_clicked=false;
	$($RTC_marker_dialog.form_inputs).each(function(){
	    $(this).val('').removeClass('ui-state-error');
	});
    },
    clear_form_errors:function() {
	$($RTC_marker_dialog.form_inputs).each(function(){
	    $(this).removeClass('ui-state-error');
	});
    },
    all_field_values:function() {
	var all_values = {};
	$($RTC_marker_dialog.form_inputs).each(function(){
	    all_values[$(this).attr('id')]=$(this).val();
	});
	return all_values;
    },
    /**
     * @param object field_values {(field id):(field value),...}
     */
    set_field_values:function(field_values) {
	if(field_values) {
	    $($RTC_marker_dialog.form_inputs).each(function(i,input){
		if(field_values[input.attr('id')]!=undefined) {
		    input.val(field_values[input.attr('id')]);
		}
	    });
	}
    },
    updateTips:function(t) {
	this.feedback_element.text(t).effect("highlight",{},1500);
    },

    checkLength:function(input_element,msg_label,min,max) {
	if ( input_element.val().length > max || input_element.val().length < min ) {
	    input_element.addClass('ui-state-error');
	    this.updateTips("Length of " + msg_label + " must be between "+min+" and "+max+".");
	    return false;
	} else {
	    return true;
	}
    },

    checkRegexp:function(input_element,regexp,error_message) {
	if ( !( regexp.test( input_element.val() ) ) ) {
	    input_element.addClass('ui-state-error');
	    this.updateTips(error_message);
	    return false;
	} else {
	    return true;
	}
    }

}